<template>
	<el-dialog :title="titleMap[mode]" v-model="visible" destroy-on-close @closed="$emit('closed')" align-center width="1200">
		<el-form :model="form" :rules="rules" ref="dialogForm" label-width="110px" label-position="right" style="height: 650px;">
            <el-container>
                <el-header>
                    <div class="left-panel">
                        <el-button type="primary" icon="el-icon-plus" @click="invoiceAdd" v-auth="'invoice.add'"></el-button>
                    </div>
                </el-header>
                <el-main class="nopadding">
                    <scTable ref="table" row-key="id" :apiObj="invoice" highlight-current-row @row-click="selectionChange" stripe remoteSort remoteFilter border :params="query" height="500">
                        <el-table-column width="1"></el-table-column>
                        <el-table-column type="selection" width="50" reserve-selection align="center"></el-table-column>
                        <el-table-column label="客户编号" prop="customer.apply_code" align="center" width="100"></el-table-column>
                        <el-table-column label="开票类型" prop="open_type" align="center" width="120">
                            <template #default="scope">
                                <div v-if="scope.row.open_type == 1">增值税纸质专票</div>
                                <div v-if="scope.row.open_type == 2">增值税纸质普票</div>
                                <div v-if="scope.row.open_type == 3">增值税电子普票</div>
                                <div v-if="scope.row.open_type == 4">增值税电子专票</div>
                            </template>
                        </el-table-column>
                        <el-table-column label="客户抬头" prop="customer_rise" align="center" width="120"></el-table-column>
                        <el-table-column label="纳税人识别号" prop="taxpayer_num" align="center" width="120"></el-table-column>
                        <el-table-column label="开户行" prop="open_bank" align="center" width="100"></el-table-column>
                        <el-table-column label="开票地址" prop="open_address" align="center" width="100"></el-table-column>
                        <el-table-column label="电话" prop="phone" align="center" width="120"></el-table-column>
                        <el-table-column label="邮箱" prop="email" align="center" width="120"></el-table-column>
                        <el-table-column label="收件人" prop="receipt" align="center" width="100"></el-table-column>
                        <el-table-column label="邮寄地址" prop="mail_address" align="center" width="120"></el-table-column>
                        <el-table-column label="手机号" prop="mobile" align="center" width="110"></el-table-column>
                        <el-table-column label="操作" fixed="right" align="center" width="120">
                            <template #default="scope">
                                <el-button-group>
                                    <el-button text type="primary" size="small" @click="invoice_table_edit(scope.row, scope.$index)" v-auth="'invoice.edit'">编辑</el-button>
                                    <el-popconfirm title="确定删除吗？" @confirm="invoice_table_del(scope.row, scope.$index)">
                                        <template #reference>
                                            <el-button text type="danger" size="small" v-auth="'invoice.delete'">删除</el-button>
                                        </template>
                                    </el-popconfirm>
                                </el-button-group>
                            </template>
                        </el-table-column>
                    </scTable>
                </el-main>
            </el-container>
		</el-form>
	</el-dialog>
    <invoice-save v-if="dialog.invoice" ref="invoiceDialog" @success="handleSuccess" @closed="dialog.invoice = false" draggable></invoice-save>
</template>

<script>
import InvoiceSave from '../../client/list/invoiceSave.vue'
export default {
    components: { InvoiceSave },
	emits: ['success', 'closed'],
    props:{
        query: { type: String, default: "" },
    },
	data() {
		return {
			mode: "add",
			titleMap: {
				add: '开票信息',
				edit: '开票信息',
				show: '客户详情'
			},
			visible: false,
			isSaveing: false,
			//表单数据
            form:{
                id:"",
                apply_code:"",
            },
            invoice:this.$API.client.invoice.list,
            dialog:{
                invoice:false,
            },
		}
	},
	mounted() {
	},
	methods: {
		//显示
		open(mode = 'add') {
			this.mode = mode;
			this.visible = true;
			return this
		},
		//表单注入数据
		async setData(data) {
            this.form.id = data.id
            this.form.apply_code = data.apply_code
		},
        selectionChange(selection){
            this.$emit('success', selection)
            this.visible = false;
        },
        // 开票
        invoiceAdd(){
            let data = {
                id:this.form.id,
                custom_code:this.form.apply_code
            }
            this.dialog.invoice = true
			this.$nextTick(() => {
				this.$refs.invoiceDialog.open('add').setId(data)
			})
        },
        // 开票编辑
        invoice_table_edit(row) {
            this.dialog.invoice = true
			this.$nextTick(() => {
				this.$refs.invoiceDialog.open('edit').setData(row)
			})
        },
        // 开票删除
        async invoice_table_del(row, index) {
			var reqData = {id: row.id}
			var res = await this.$API.client.invoice.delete.post(reqData);
			if (res.code == 1) {
				this.$refs.table.tableData.splice(index, 1);
                this.$refs.table.refresh()
				this.$message.success("删除成功")
			} else {
				this.$alert(res.message, "提示", {type: 'error'})
			}
		},
        // 数据更新
        handleSuccess(data, mode) {
			if (mode == 'add') {
				this.$refs.table.refresh()
			} else if (mode == 'edit') {
				this.$refs.table.refresh()
			}
		},
	}
}
</script>

<style></style>
